package com.hunliji.monitor.web.strategy.interception.handler.impl;

import com.alibaba.fastjson.JSONObject;
import com.hunliji.monitor.share.component.mq.rocket.RocketMqFacade;
import com.hunliji.monitor.share.component.mq.rocket.constants.RocketMqConstants;
import com.hunliji.monitor.share.enums.feishu.InterceptEventTypeEnum;
import com.hunliji.monitor.share.enums.feishu.WorkItemKeyTypeEnum;
import com.hunliji.monitor.web.model.param.feishu.InterceptPluginContextParam;
import com.hunliji.monitor.web.strategy.interception.handler.AbstractFeishuInterceptionHandler;
import com.hunliji.monitor.web.strategy.interception.param.FeishuInterceptionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

@Service
@Slf4j
public class CreateWorkItemHandler extends AbstractFeishuInterceptionHandler {

    @Resource
    private RocketMqFacade rocketMqFacade;

    @Override
    public InterceptEventTypeEnum getEventType() {
        return InterceptEventTypeEnum.CREATE_WORK_ITEM;
    }

    @Override
    public void process(FeishuInterceptionContext context) {
        if (context == null) {
            log.warn("缺失上下文");
            return;
        }
        InterceptPluginContextParam param = context.getParam();
        if (CollectionUtils.isEmpty(param.getWorkItemInfo())) {
            log.warn("无工作项信息");
            return;
        }

        if (!WorkItemKeyTypeEnum.ISSUE.getTypeKey().equals(param.getWorkItemTypeKey())) {
            // 只处理缺陷
            return;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("workItemName", param.getWorkItemInfo().get(0).getWorkItemName());
        jsonObject.put("workItemTypeKey", param.getWorkItemTypeKey());
        jsonObject.put("projectKey", param.getProjectKey());

        rocketMqFacade.delayedSend(RocketMqConstants.TOPIC_FEISHU_CREATE_ISSUE_DELAY, RocketMqConstants.TAG_FEISHU_CREATE_ISSUE_DELAY, jsonObject.toJSONString(), RocketMqConstants.DelayLevel.TEN_SECOND);
    }



}
